using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CPGVertragsverwaltung.SecurityController;
using System.Diagnostics;
using Infragistics.Win.UltraWinListView;
using Infragistics.Win.UltraWinTree;

namespace CPGVertragsverwaltung.Viewer
{
    public partial class SecurityObjectSelector : Form
    {
        private Boolean mIsExpanded = false;

        public SecurityObjectSelector()
        {
            InitializeComponent();
        }

        private void loadADObjects()
        {
            //String GroupName = ADServices.getNameFromGUID(ADServices);
            SecurityObjectsCollection myObjects = ADServices.getChildObjects(GlobalVariables.ADUserGroupSID);

            //Sorting List : First Groups then Users
            for (int i = 0; i < myObjects.Count; i++)
            {
                StringCollection tempCollection1 = myObjects[i];
                if (tempCollection1[2].ToLower() == "user")
                {
                    for (int j = i + 1; j < myObjects.Count; j++)
                    {
                        StringCollection tempCollection2 = myObjects[j];
                        //exchange Position
                        if (tempCollection2[2].ToLower() == "group")
                        {
                            StringCollection tempCollection3 = myObjects[i]; 
                            //tempCollection3 = myObjects[i];
                            myObjects[i] = myObjects[j];
                            myObjects[j] = tempCollection3;
                            break;
                        }
                        
                    }
                }
            }

            foreach (StringCollection myAttributes in myObjects)
            {
                //Add Item to ListView
                String[] subItems = new String[2];
                subItems[0] = myAttributes[0]; subItems[1] = myAttributes[2].ToLower();
                UltraListViewItem myItem = new UltraListViewItem(myAttributes[1], subItems);
                //Icon
                switch (myAttributes[2].ToLower())
                {
                    case "group":
                        myItem.Appearance.Image = global::CPGVertragsverwaltung.Properties.Resources.IconGroup;
                        break;
                    case "user":
                        myItem.Appearance.Image = global::CPGVertragsverwaltung.Properties.Resources.IconUser;
                        break;
                }
                lstSecurityObject.Items.Add(myItem);
                Application.DoEvents();
            }
        }

        private void SecurityObjectSelector_Shown(object sender, EventArgs e)
        {
            Application.DoEvents();
            loadADObjects();
        }

        public SecurityObjectsCollection selectedObjects
        {
            get
            {
                SecurityObjectsCollection result = new SecurityObjectsCollection();
                foreach (UltraListViewItem myItem in lstSecurityObject.SelectedItems)
                {
                    StringCollection myAttributes = new StringCollection();
                    myAttributes.Add(myItem.SubItems[0].Text);
                    myAttributes.Add(myItem.Text);
                    myAttributes.Add(myItem.SubItems[1].Text);
                    result.Add(myAttributes);
                }
                return result;
            }
        }

        private void SecurityObjectSelector_Load(object sender, EventArgs e)
        {

        }

        private void bttExpand_Click(object sender, EventArgs e)
        {
            if (mIsExpanded)
            {
                this.Width = 375;
                bttExpand.Text = ">>>>>>>>>>";
                mIsExpanded = false;
            }
            else
            {
                this.Width = 700;
                bttExpand.Text = "<<<<<<<<<<";
                mIsExpanded = true;
            }
        }

        /// <summary>
        /// Add Parent item to TreeList
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstSecurityObject_ItemSelectionChanged(object sender, ItemSelectionChangedEventArgs e)
        {
            treeChildren.Nodes.Clear();

            if (lstSecurityObject.SelectedItems.Count < 1)
                return;

            UltraListViewItem selectedItem = lstSecurityObject.SelectedItems[0];
            String selectedSID = selectedItem.SubItems[0].Text;
            String selectedName = selectedItem.Text;
            String selectedType = selectedItem.SubItems[1].Text;

            UltraTreeNode MainNode = new UltraTreeNode("", selectedName);
            if (selectedType.ToLower() == "group")
            {
                MainNode.LeftImages.Add(global::CPGVertragsverwaltung.Properties.Resources.IconGroup);
            }
            else
            {
                MainNode.LeftImages.Add(global::CPGVertragsverwaltung.Properties.Resources.IconUser);
            }
            treeChildren.Nodes.Add(MainNode);
            Application.DoEvents();
            addChildrenNodes(MainNode, selectedSID);
        }

        /// <summary>
        /// Search in Active Directory for Children and Add it to Tree (recursive Calls)
        /// </summary>
        /// <param name="pParentNode"></param>
        private void addChildrenNodes(UltraTreeNode pParentNode, String pSID)
        {
            SecurityObjectsCollection myCollection = ADServices.getDirectChildrenFromSID(pSID);
            foreach (StringCollection tempChild in myCollection)
            {
                String childSID = tempChild[0];
                String childName = tempChild[1];
                String childType = tempChild[2];

                UltraTreeNode ChildNode = new UltraTreeNode("", childName);
                //Image
                if (childType.ToLower() == "group")
                {
                    ChildNode.LeftImages.Add(global::CPGVertragsverwaltung.Properties.Resources.IconGroup);
                }
                else
                {
                    ChildNode.LeftImages.Add(global::CPGVertragsverwaltung.Properties.Resources.IconUser);
                }

                try
                {
                    pParentNode.Nodes.Add(ChildNode);
                    Application.DoEvents();
                    addChildrenNodes(ChildNode, childSID);
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
}
